package com.qykj.ddhing.pda.demo.databinding;

import android.app.Activity;
import android.databinding.Observable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.ArrayMap;


/**
 * author : 0000
 * desc   :
 * time   : 2018/07/20
 * updateTime : 2018/07/20
 */
public class DataBindingUtils {
    private static ArrayMap<Activity, ArrayMap<Observable, Observable.OnPropertyChangedCallback>> commonMap = new ArrayMap<>();

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static void addCallBack(Activity activity,
                                   Observable observable, Observable.OnPropertyChangedCallback callback) {
        ArrayMap<Observable, Observable.OnPropertyChangedCallback> callbackArrayMap = commonMap.get(activity);
        if (callbackArrayMap == null) {
            callbackArrayMap = new ArrayMap<>();
            commonMap.put(activity, callbackArrayMap);
        }
        observable.addOnPropertyChangedCallback(callback);
        callbackArrayMap.put(observable, callback);
    }

    public static void removeCallBack(Activity activity) {
        ArrayMap<Observable, Observable.OnPropertyChangedCallback> callbackArrayMap = commonMap.get(activity);
        if (callbackArrayMap != null) {
            for (Observable observable : callbackArrayMap.keySet()) {
                observable.removeOnPropertyChangedCallback(callbackArrayMap.get(observable));
            }
        }
    }

}
